import { _decorator, Component, Node, Animation, Enum, sys, native } from "cc";
// import { EventMgr } from "../../events/eventmgr";
// import { EventName } from "../../events/EventName";
// import { UIMgr } from "../../Lobby/Scripts/UIMgr";


export default class Google {

    /**
     * 分享类型
     */
    public static SHARE_TYPE = {
        /**
         * 链接
         */
        LINK: 0,
        /**
         * 图片
         */
        PHOTO: 1
    }

    /**
     * 登录
     */
    public static login() {
        console.log("Google login");
        if (sys.os == sys.OS.IOS) {
            console.log("Google login1111111");
            let className = "AppDelegate";
            let methodName = "googleLogin:";
            native.reflection.callStaticMethod(className, methodName, "Google login");
        }
        if (sys.isNative) {
            native.jsbBridgeWrapper.dispatchEventToNative("loginGoogle");
            // if (sys.os == sys.OS.ANDROID) {
            //     let className = "com/util/BridgeUtil";
            //     let methodName = "GoogleLogin";
            //     let methodSignature = "()V";
            //     jsb.reflection.callStaticMethod(className, methodName, methodSignature);
            // } else if (sys.os == sys.OS.IOS) {
            //     let className = "BridgeUtil";
            //     let methodName = "GoogleLogin";
            //     jsb.reflection.callStaticMethod(className, methodName,"");
            // }
        } else {
            let authInfo = '{"id":"1111111111111111","name":"Test","gender":"","email":"123456@qq.com","picture":"https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=xxx&height=50&width=50&ext=xxx","avatar":"https://graph.Google.com/xxx/picture?type=large"}';
            onGoogleLoginSuccess(authInfo);
        }
    }



    /**
     * 登出
     */
    public static logout() {
        console.log("Google logout");

        if (sys.isNative) {
            native.jsbBridgeWrapper.dispatchEventToNative("logOutGoogle");
            // if (sys.os == sys.OS.ANDROID) {
            //     let className = "com/util/BridgeUtil";
            //     let methodName = "GoogleLogout";
            //     let methodSignature = "()V";
            //     jsb.reflection.callStaticMethod(className, methodName, methodSignature);
            // } else if (sys.os == sys.OS.IOS) {
            //     let className = "BridgeUtil";
            //     let methodName = "GoogleLogout";
            //     jsb.reflection.callStaticMethod(className, methodName,"");
            // }
        }
    }

    /**
     * google广告
     */
    public static async startadvertising() {
        //开始加载广告

        if (sys.os == sys.OS.IOS) {
            // await UIMgr.getInstance().showWaiting();
            console.log("Google login1111111");
            let className = "AppDelegate";
            let methodName = "googleAdvertising:";
            native.reflection.callStaticMethod(className, methodName, "Google startadvertising");
        } else if (sys.os == sys.OS.ANDROID) {
            // await UIMgr.getInstance().showWaiting();
            native.jsbBridgeWrapper.dispatchEventToNative("googleAdvertising");
        }
    }


    public static registerGoogleScriptEvents() {
        //google ads 广告的回调
        //谷歌广告回调，暂时加在这里需要时复制
        native.jsbBridgeWrapper.addNativeEventListener("googleAdvertisingCall", (datas: string) => {
            Google.googleAdvertisingCall(datas);
        });
    }

    /**
     * 21点击广告单独处理奖励，22关闭广告，如果之前收到过24不弹窗，没收到过弹出还没有播放完成，23，广告加载错误，24广告观看完成
     * @param data 
     */
    public static async googleAdvertisingCall(data: string) {
        let jsonData = JSON.parse(data);
        console.log("google广告回调");
        let result = jsonData["result"].toString();
        console.log(result);
        //result  21点击广告  22 关闭广告  23 打开广告失败
        if (result.toString() == "21") {

        } else if (result.toString() == "22") {
            // EventMgr.getInstance().dispatchEvent(EventName.Notify_Ads_Close);
        } else if (result.toString() == "23") {
            // await UIMgr.getInstance().clearWaiting();
            // UIMgr.getInstance().AlertTxt("获取广告失败");
        } else if (result.toString() == "24") {
            // UIMgr.getInstance().clearWaiting();
        }
    }
    /**
     * 支付
     */

}

/**
 * 登录成功
 */
const onGoogleLoginSuccess = function (authInfo: any) {
    console.log("onGoogleLoginSuccess authInfo = " + authInfo);
    //处理登录逻辑
};

/**
 * 登录取消
 */
const onGoogleLoginCancel = function () {
    console.log("onGoogleLoginCancel");
};

/**
 * 登录错误
 */
const onGoogleLoginError = function (error: any) {
    console.log("onGoogleLoginError error = " + error);
};





